<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="proCode">
        <el-input v-model="queryParams.proCode" placeholder="请输入商品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="捐赠来源" prop="sourceId">
        <el-select v-model="queryParams.sourceId" filterable placeholder="请选择捐赠来源" style="">
          <el-option v-for="item in donate_option" :key="item.id" :label="item.donateName" :value="item.id"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="出入库标志" prop="inventoryFlag" label-width="90px">
        <el-input v-model="queryParams.inventoryFlag" placeholder="请输入出入库标志" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['supermarket:inventory:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supermarket:inventory:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['supermarket:inventory:remove']">删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supermarket:inventory:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column type="index" width="50" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品编码" align="center" prop="proCode" />
      <el-table-column label="规格" align="center" prop="specifications" />
      <el-table-column label="数量" align="center" prop="outboundNumber" />
      <el-table-column label="捐赠来源" align="center" prop="sourceNames">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in scope.row.sourceNames" :key="index" type="info" style="margin-right: 10px">
            {{ item }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="出库原因" align="center" prop="outboundReason" />
      <el-table-column label="出库时间" align="center" prop="updateTime" width="180">
      </el-table-column>
      <el-table-column label="出库操作人" align="center" prop="updateBy" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="商品编码" prop="proCode" >
          <div style="display: flex;">
             <el-input v-model="form.proCode" placeholder="请输入商品编码" style="margin-right: 10px;"/>
          <el-button type="primary" size="mini" @click="handleQueryByCode">搜索</el-button>
          </div>

        </el-form-item>
        <el-form-item label="出库数量" prop="outboundNumber">
          <el-input-number v-model="form.outboundNumber" :min="1" :max="form.inventory"></el-input-number>
          <span v-show="form.inventory" style="color: #b9b9ba;margin-left: 20px;">剩余库存量：{{ form.inventory }}</span>
        </el-form-item>
        <el-form-item label="出库原因" prop="outboundReason">
          <el-input v-model="form.outboundReason" placeholder="请输入出库原因" />
        </el-form-item>
        <span v-show="form.productName"
          style="font-size: 20px;font-weight: 700;margin-top: 10px;margin-bottom: 10px;">商品信息</span>
        <el-form-item v-show="form.productName" label="商品名称" prop="productName">
          <el-input v-model="form.productName" disabled />
        </el-form-item>
        <el-form-item v-show="form.color" label="颜色" prop="color">
          <el-input v-model="form.color" disabled />
        </el-form-item>
        <el-form-item v-show="form.size" label="尺码" prop="size">
          <el-input v-model="form.size" disabled />
        </el-form-item>
        <el-form-item v-show="form.timeSlot" label="时间段" prop="timeSlot">
          <el-input v-model="form.timeSlot" disabled />
        </el-form-item>
        <!-- <el-form-item label="商品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品id" />
        </el-form-item> -->
        <el-form-item v-show="form.specifications" label="规格" prop="specifications">
          <el-input v-model="form.specifications" disabled />
        </el-form-item>
        <el-form-item v-show="form.timeInterval" label="时间区间" prop="timeInterval">
          <el-input v-model="form.timeInterval" disabled />
        </el-form-item>
        <el-form-item v-show="form.integratedWorth" label="积分价值" prop="integratedWorth">
          <el-input v-model="form.integratedWorth" disabled />
        </el-form-item>
        <!-- <el-form-item v-show="form.productName" label="捐赠来源" prop="sourceId">
          <el-input v-model="form.sourceId" disabled/>
        </el-form-item> -->
        <!-- <el-form-item v-show="form.productName" label="所属地区" prop="platformId">
          <el-input v-model="form.platformId"  disabled/>
        </el-form-item> -->
        <el-form-item v-show="form.inventoryFlag" label="出入库标志" prop="inventoryFlag">
          <el-input v-model="form.inventoryFlag" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getInventory, delInventory, addInventory, updateInventory, addOutInventory,getInventoryByCode } from "@/api/supermarket/inventory";
import { donateList } from '@/api/donate/source'
// import { getProductByBarcode } from '@/api/supermarket/scencExchange'

export default {
  name: "Inventory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品库存表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proCode: null,
        productName: null,
        sourceId: null,
        inventoryFlag: 2,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //捐赠来源list
      donate_option: [],
    };
  },
  created() {
    this.getList();
    this.getDonateList()
  },
  methods: {
    /** 查询商品库存列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.pageData;
        console.log(this.inventoryList)
        this.total=parseInt(response.total) ;
        this.loading = false;
      });
    },
    //获取捐赠来源list
    getDonateList() {
      donateList().then(response => {
        this.donate_option = response.data
      })
    },
    // 根据code查询商品
    handleQueryByCode() {
      // if(parseInt(this.form.proCode).length)
      getInventoryByCode({ proCode: this.form.proCode }).then(res => {
        if (res.code == 200) {
          console.log(res)
          this.form.productName = res.data.productName
          this.form.createBy = res.data.createBy
          this.form.inventory = res.data.inventory
          this.form.inventoryFlag = res.data.inventoryFlag
          if(res.data.color){
            this.form.color = res.data.color
          }
          if(res.data.size){
            this.form.size = res.data.size
          }
          if(res.data.specifications){
            this.form.specifications = res.data.specifications
          }

          this.form.timeSlot = res.data.timeSlot
          this.form.timeInterval = res.data.timeInterval
          this.form.integratedWorth = res.data.integratedWorth
          this.form.updateTime = res.data.updateTime
          // this.form.sourceId=res.data.sourceId
        }
      }).catch(()=>{
        this.form.proCode=''
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        color: null,
        integratedWorth: null,
        inventory: null,
        inventoryFlag: null,
        outboundNumber: null,
        outboundReason: null,
        proCode: null,
        productName: null,
        size: null,
        sourceId: null,
        specifications: null,
        timeInterval: null,
        timeSlot: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams)
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品库存";
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getInventory(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改商品库存";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.id != null) {
          //   updateInventory(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          addOutInventory(JSON.stringify(this.form)).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除商品库存编号为"' + ids + '"的数据项？').then(function () {
    //     return delInventory(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => { });
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supermarket/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
